using UnityEngine;
using UnityEditor;

public class GraphicsAPIChecker : MonoBehaviour
{
    [System.Serializable]
    public class GraphicsAPIInfo
    {
        public string platform;
        public string[] apis;
        public string currentAPI;
        public bool isVulkanSupported;
        public bool isOpenGLSupported;
    }

    public GraphicsAPIInfo[] graphicsAPIInfos;

    void Start()
    {
        CheckGraphicsAPIs();
    }

    /// <summary>
    /// 检查当前Graphics API设置
    /// </summary>
    public void CheckGraphicsAPIs()
    {
        Debug.Log("=== Graphics API 检查 ===");
        
        // 检查当前平台
        string currentPlatform = Application.platform.ToString();
        Debug.Log($"当前平台: {currentPlatform}");
        
        // 检查当前Graphics API
        string currentAPI = SystemInfo.graphicsDeviceType.ToString();
        Debug.Log($"当前Graphics API: {currentAPI}");
        
        // 检查支持的Graphics API
        Debug.Log("支持的Graphics API:");
        foreach (UnityEngine.Rendering.GraphicsDeviceType api in System.Enum.GetValues(typeof(UnityEngine.Rendering.GraphicsDeviceType)))
        {
            if (SystemInfo.graphicsDeviceType == api)
            {
                Debug.Log($"  ✓ {api} (当前使用)");
            }
            else
            {
                Debug.Log($"  - {api}");
            }
        }
        
        // 检查Vulkan支持
        bool vulkanSupported = SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Vulkan;
        Debug.Log($"Vulkan支持: {(vulkanSupported ? "是" : "否")}");
        
        // 检查OpenGL支持
        bool openGLSupported = SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLCore ||
                              SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES3 ||
                              SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2;
        Debug.Log($"OpenGL支持: {(openGLSupported ? "是" : "否")}");
        
        // 检查WebView相关设置
        CheckWebViewSettings();
    }

    /// <summary>
    /// 检查WebView相关设置
    /// </summary>
    private void CheckWebViewSettings()
    {
        Debug.Log("=== WebView 相关设置 ===");
        
        // 检查渲染管线
        var renderPipelineAsset = UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset;
        if (renderPipelineAsset != null)
        {
            Debug.Log($"渲染管线: {renderPipelineAsset.GetType().Name}");
        }
        else
        {
            Debug.Log("渲染管线: 内置渲染管线");
        }
        
        // 检查SRP Batcher设置
        bool srpBatcherEnabled = UnityEngine.Rendering.GraphicsSettings.useScriptableRenderPipelineBatching;
        Debug.Log($"SRP Batcher: {(srpBatcherEnabled ? "启用" : "禁用")}");
        
        // 检查多线程渲染
        bool multithreadedRendering = UnityEngine.Rendering.GraphicsSettings.useScriptableRenderPipelineBatching;
        Debug.Log($"多线程渲染: {(multithreadedRendering ? "启用" : "禁用")}");
    }

    /// <summary>
    /// 建议的Graphics API设置
    /// </summary>
    public void SuggestGraphicsAPISettings()
    {
        Debug.Log("=== Graphics API 建议 ===");
        
        if (Application.platform == RuntimePlatform.Android)
        {
            Debug.Log("Android平台建议:");
            
            if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Vulkan)
            {
                Debug.Log("  ✓ 推荐使用 Vulkan (更好的WebView兼容性)");
                Debug.Log("    设置路径: Edit > Project Settings > Player > Other Settings > Graphics APIs");
                Debug.Log("    将Vulkan移到第一位");
            }
            else
            {
                Debug.Log("  ⚠ Vulkan不支持，使用OpenGL ES 3.0");
            }
            
            Debug.Log("  ✓ 禁用SRP Batcher (可能影响WebView纹理显示)");
            Debug.Log("    设置路径: Universal Render Pipeline Asset > Advanced > SRP Batcher");
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            Debug.Log("iOS平台建议:");
            Debug.Log("  ✓ 使用 Metal (默认且最佳)");
        }
        else
        {
            Debug.Log("其他平台建议:");
            Debug.Log("  ✓ 使用默认Graphics API");
        }
    }

    /// <summary>
    /// 检查是否可能导致WebView问题的设置
    /// </summary>
    public void CheckWebViewIssues()
    {
        Debug.Log("=== WebView 潜在问题检查 ===");
        
        bool hasIssues = false;
        
        // 检查当前Graphics API
        string currentAPI = SystemInfo.graphicsDeviceType.ToString();
        if (currentAPI.Contains("OpenGL") && Application.platform == RuntimePlatform.Android)
        {
            Debug.LogWarning("⚠ 当前使用OpenGL，可能导致eglCodecCommon错误");
            hasIssues = true;
        }
        
        // 检查SRP Batcher
        bool srpBatcherEnabled = UnityEngine.Rendering.GraphicsSettings.useScriptableRenderPipelineBatching;
        if (srpBatcherEnabled && Application.platform == RuntimePlatform.Android)
        {
            Debug.LogWarning("⚠ SRP Batcher已启用，可能影响WebView纹理显示");
            hasIssues = true;
        }
        
        // 检查渲染管线
        var renderPipelineAsset = UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset;
        if (renderPipelineAsset != null && Application.platform == RuntimePlatform.Android)
        {
            Debug.LogWarning("⚠ 使用URP/HDRP，需要特殊配置以支持WebView");
            hasIssues = true;
        }
        
        if (!hasIssues)
        {
            Debug.Log("✓ 未发现明显的WebView兼容性问题");
        }
    }

    /// <summary>
    /// 在Lua中调用的方法
    /// </summary>
    public void LogGraphicsInfo()
    {
        CheckGraphicsAPIs();
        SuggestGraphicsAPISettings();
        CheckWebViewIssues();
    }
}

#if UNITY_EDITOR
/// <summary>
/// 编辑器工具类，用于修改Graphics API设置
/// </summary>
public class GraphicsAPIEditor
{
    [MenuItem("Tools/WebView/检查Graphics API设置")]
    public static void CheckGraphicsAPISettings()
    {
        var checker = new GameObject("GraphicsAPIChecker").AddComponent<GraphicsAPIChecker>();
        checker.CheckGraphicsAPIs();
        checker.SuggestGraphicsAPISettings();
        checker.CheckWebViewIssues();
        Object.DestroyImmediate(checker.gameObject);
    }

    [MenuItem("Tools/WebView/打开Player Settings")]
    public static void OpenPlayerSettings()
    {
        EditorApplication.ExecuteMenuItem("Edit/Project Settings...");
        EditorApplication.ExecuteMenuItem("Window/General/Project Settings");
    }

    [MenuItem("Tools/WebView/打开Graphics Settings")]
    public static void OpenGraphicsSettings()
    {
        EditorApplication.ExecuteMenuItem("Edit/Project Settings...");
        EditorApplication.ExecuteMenuItem("Window/General/Project Settings");
    }
}
#endif